package com.azt.provider.impl;

import com.azt.api.pojo.Meeting;
import com.azt.api.pojo.req.MeetingReq;
import com.azt.api.service.MeetingService;
import com.azt.model.page.Pagination;
import com.azt.provider.mapper.MeetingMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 会议管理
 * Created by LiQZ on 2017/4/6.
 */
@Service
public class MeetingServiceImpl implements MeetingService {

    @Autowired
    private MeetingMapper meetingMapper;

    @Override
    public Pagination getPage(int pageNo, int pageSize, Map<String, String> searchMap) {
        Page<Object> startPage = PageHelper.startPage(pageNo, pageSize);
        PageHelper.orderBy("createtime desc");
        List<Meeting> list = meetingMapper.selectAll();
        Pagination p = new Pagination(startPage, list);
        p.setList(list);
        p.setParams(searchMap);
        return p;
    }

    @Override
    public void delete(Integer id) {
        meetingMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void updateToCurrent(Integer id) {
        meetingMapper.updateAllState();
        meetingMapper.updateState(id);
    }

    @Override
    public void save(MeetingReq req) {
        Meeting meeting = new Meeting();
        BeanUtils.copyProperties(req, meeting);
        if (req.getId() != null) {
            meetingMapper.updateByPrimaryKeySelective(meeting);
        } else {
            meeting.setState(0);
            meetingMapper.insert(meeting);
        }
    }

    @Override
    public Meeting getDefaultMeeting() {
        return meetingMapper.selectDefault();
    }
}
